package sbc.group1.lab1.common;

import java.io.Serializable;

public class Part implements Serializable, Identifieable {
    
    private int id;
    private final int producerId;
    private final boolean isDefect;



    public Part(int id, Producer producer, boolean isDefect) {
        this.id = id;
        this.producerId = producer.getId();
        this.isDefect=isDefect;
    }
    
    public Part(Producer producer, boolean isDefect) {
        this.id = NO_ID;
        this.producerId = producer.getId();
        this.isDefect=isDefect;
    }
    
    @Deprecated
    public Part(int id, Producer producer) {
        this(id, producer, false);
    }
    @Deprecated
    public Part(Producer producer) {
        this(NO_ID, producer, false);
    }
    
    public int getProducerId() {
        return producerId;
    }
    
    public final boolean hasId() {
        return id != NO_ID;
    }
    
    public final boolean setId(int id) {
        if (hasId()) {
            return false;
        }else {
            this.id = id;
            return true;
        }
    }
    
    public final int getId() {
        return id;
    }
    
    public boolean isDefect() {
        return isDefect;
    }
    
    public String toString() {
        return this.getClass().getSimpleName()+ "[id="+getId()+
        ", defect="+isDefect()+", producerId="+getProducerId()+"]";
    }
    
    public boolean equals(Object o) {
        if (o != null) {
            if (o instanceof Part) {
                Part part = (Part)o;
                if (part.getId() == this.getId()) {
                    return true;
                }
            }
        }
        return false;
    }
    
    public int hashCode() {
        return id;
    }
}
